import os
import time
import shutil

from code_builder.conf import read_conf
from code_builder.tools import utils

def group():
    now_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
    group_base_path = read_conf.getConfig("group","group_base_path")
    list = os.listdir(group_base_path)
    group_target_path = read_conf.getConfig("group","group_target_path") + "\\"+now_time
    utils.create_dir(group_target_path)
    dir_list = ['dao','service','controller','entity','mapper','views']
    for i in dir_list:
        utils.create_dir(group_target_path+"\\"+i)
    for i in list:
        path = group_base_path+"\\"+i
        target_path=group_target_path
        if os.path.isdir(path):
            file_list = os.listdir(group_base_path+"\\"+i)
            for j in file_list:
                file_path = path+"\\"+j
                suffix = os.path.splitext(file_path)[1]
                if suffix == ".jsp":
                    shutil.copy(file_path,target_path+"\\views\\"+j)
                elif suffix == ".xml":
                    shutil.copy(file_path,target_path+"\\mapper\\"+j)
                else:
                    print(file_path[-15:-5])
                    if file_path[-15:-5] == "Controller":
                        shutil.copy(file_path, target_path + "\\controller\\" + j)
                    elif file_path[-11:-5] == "Mapper":
                        shutil.copy(file_path, target_path + "\\dao\\" + j)
                    elif file_path[-12:-5] == "Service" or file_path[-16:-5] == "ServiceImpl":
                        shutil.copy(file_path, target_path + "\\service\\" + j)
                    else:
                        shutil.copy(file_path, target_path + "\\entity\\" + j)
        elif os.path.isfile(path):
            target_path=target_path
            shutil.copy(path,target_path+"\\"+i)

if __name__=="__main__":
    group()
    print("已成功分组")